package com.gage.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gage.commons.result.PageInfo;
import com.gage.commons.utils.BeanUtils;
import com.gage.commons.utils.StringUtils;
import com.gage.mapper.BsDepartmentUserMapper;
import com.gage.mapper.SysUserMapper;
import com.gage.mapper.SysUserRoleMapper;
import com.gage.model.BsDepartmentUser;
import com.gage.model.SysUser;
import com.gage.model.SysUserRole;
import com.gage.model.vo.UserVo;
import com.gage.service.IUserService;

/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements IUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private BsDepartmentUserMapper departmentUserMapper;
    private Logger log = Logger.getLogger(UserServiceImpl.class);
    @Override
    public List<SysUser> selectByLoginName(UserVo userVo) {
        SysUser sysUser = new SysUser();
        sysUser.setLoginName(userVo.getLoginName());
        EntityWrapper<SysUser> wrapper = new EntityWrapper<SysUser>(sysUser);
        if (null != userVo.getId()) {
            wrapper.where("id != {0}", userVo.getId());
        }
        return this.selectList(wrapper);
    }

    @Override
    public void insertByVo(UserVo userVo) {
        SysUser sysUser = BeanUtils.copy(userVo, SysUser.class);
        sysUser.setCreateTime(new Date());
        this.insert(sysUser);
        
        Long id = sysUser.getId();
        String[] roles = userVo.getRoleIds().split(",");
        SysUserRole sysUserRole = new SysUserRole();
        for (String string : roles) {
            sysUserRole.setUserId(id);
            sysUserRole.setRoleId(Long.valueOf(string));
            sysUserRoleMapper.insert(sysUserRole);
        }
    }

    @Override
    public UserVo selectVoById(Long id) {
        return sysUserMapper.selectUserVoById(id);
    }

    @Override
    public void updateByVo(UserVo userVo) {
        SysUser sysUser = BeanUtils.copy(userVo, SysUser.class);
        if (StringUtils.isBlank(sysUser.getPassword())) {
            sysUser.setPassword(null);
        }
        this.updateById(sysUser);
        
        Long id = userVo.getId();
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectByUserId(id);
        if (sysUserRoles != null && !sysUserRoles.isEmpty()) {
            for (SysUserRole sysUserRole : sysUserRoles) {
                sysUserRoleMapper.deleteById(sysUserRole.getId());
            }
        }

        String[] roles = userVo.getRoleIds().split(",");
        SysUserRole sysUserRole = new SysUserRole();
        for (String string : roles) {
            sysUserRole.setUserId(id);
            sysUserRole.setRoleId(Long.valueOf(string));
            sysUserRoleMapper.insert(sysUserRole);
        }
    }

    @Override
    public void updatePwdByUserId(Long userId, String md5Hex) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setPassword(md5Hex);
        this.updateById(sysUser);
    }

    @Override
    public PageInfo selectDataGrid(PageInfo pageInfo) {
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Map<String, Object>> list = sysUserMapper.selectUserPage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        log.error("========UserServiceImpl======="+list.toString());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public void deleteUserById(Long id) {
        this.deleteById(id);
        sysUserRoleMapper.deleteByUserId(id);
    }

	@Override
	public BsDepartmentUser selectDepartmentIdByUserIdAndType(Integer userId,
			Integer type) {
		return departmentUserMapper.selectDepartmentIdByUserIdAndType(userId, type);
	}
}